package com.fei.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShrioConfig {

    @Bean
    public AdminRealm adminRealm(){
        return new AdminRealm();
    }

    @Bean("securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("adminRealm") AdminRealm adminRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联AdminRealm
        securityManager.setRealm(adminRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        //拦截
        Map<String,String> filter = new LinkedHashMap<>();
        //权限
        filter.put("/admin/add/*","perms[admin:addUpdate]");
        filter.put("/admin/update/*","perms[admin:addUpdate]");
        filter.put("/admin/delete/*","perms[admin:delete]");
        //注销
        filter.put("/logout","logout");

        filter.put("/admin/*","authc");
        bean.setFilterChainDefinitionMap(filter);
        //设置登录请求
        bean.setLoginUrl("/");

        //设置未授权请求
        bean.setUnauthorizedUrl("/toUnauthorized");

        return bean;

    }


    //整合shiroDialect:用来整合 shiro thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }


}
